﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MaxLeaf.Common;
using System.Data;
using MaxLeaf.DB;

namespace MaxLeaf.DAL
{
    public class t_barcodeDALBuilder : BaseDALBuilder
    {


        public t_barcodeDALBuilder()
            : base()
        {
            K3DAL = new K3.t_barcodeDAL();

            WebDAL = null;// new Web.t_Base_EmpDAL();

        }
        protected override bool ValidCheckOrgData(ServerInfo orgServer, DataRowView drvLog)
        {
            if (!orgServer.MasterDB)
            {
                //主账套分发
                return false;
            }


            return base.ValidCheckOrgData(orgServer, drvLog);
        }

        protected override bool ValidCheck(ServerInfo orgServer, ServerInfo destServer, DataRowView drvLog)
        {
            if (destServer.Type== ServerType.Web )
            {
                //分发k3
                return false;
            }
            if ((LogStatus)CvtFunc.ToInt(drvLog[DEL.LogDEL.LogStatus]) == LogStatus.Delete)
            {
                return true;
            }
            using (DBAccess dbOrgCon = new DBAccess(orgServer),dbDestCon = new DBAccess(destServer))
            {
                string strSql = "select a.fnumber  from t_icitem a inner join t_barcode b on a.fitemid=b.fitemid  where b.FTypeID=4 and b.fid=@fid";
                KeyValuePair<string, IDataParameter[]> kvp = dbOrgCon.CreateCondParam(new Dictionary<string, object>() { { "fid",drvLog[DEL.LogDEL.KeyValue]}});
                string strFnumber = dbOrgCon.GetValue(strSql,kvp.Value);

                strSql = "select fnumber from t_icitem where fnumber=@fnumber";
                kvp = dbDestCon.CreateCondParam(new Dictionary<string, object>() { { "fnumber", strFnumber } });
                if (dbDestCon.GetDataFirstRow(strSql,kvp.Value)==null)
                {
                    LogManager.Debug(string.Format("barcode did not doaction for {0}", strFnumber));
                    return false;
                }

            }


            return base.ValidCheck(orgServer, destServer, drvLog);
        }


    }
}